package com.freesky.web.manager.controller;

import com.freesky.common.dao.entity.Member;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.pojo.Result;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.vo.VOMember;
import com.freesky.web.manager.remote.MemberRemote;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author Freesky
 * @date 2017/8/11
 */
@Controller
@Api(description = "会员管理")
public class MemberController {

    @Autowired
    private MemberRemote memberRemote;

    @ApiOperation(value = "进入页面-会员列表")
    @GetMapping("/member/page")
    public String memberPage() {
        return "member/list";
    }

    @ApiOperation(value = "进入页面-会员添加")
    @GetMapping("/member/add/page")
    public String memberAddPage() {
        return "member/add";
    }

    @ApiOperation(value = "进入页面-会员修改")
    @GetMapping("/member/update/page")
    public String memberUpdatePage() {
        return "member/update";
    }
    @ApiOperation(value = "进入页面-会员信息查看")
    @GetMapping("/member/info/page")
    public String memberInfoPage() {
        return "member/info";
    }
    @ApiOperation(value = "进入页面-会员删除列表")
    @GetMapping("/member/del/page")
    public String memberDelPage() {
        return "member/del-list";
    }

    @ResponseBody
    @GetMapping(value = "/member/list")
    @ApiOperation(value = "分页多条件搜索获取会员列表")
    public DataTablesResult getMemberList(
            @RequestParam(value = "draw") int draw,
            @RequestParam(value = "start") int start,
            @RequestParam(value = "length") int length,
            @RequestParam(value = "searchKey") String searchKey,
            @RequestParam(value = "minDate") String minDate,
            @RequestParam(value = "maxDate") String maxDate,
            @RequestParam(value = "search[value]") String search,
            @RequestParam(value = "order[0][column]") int orderCol,
            @RequestParam(value = "order[0][dir]") String orderDir) {
        return memberRemote.getMemberList(draw, start, length, searchKey, minDate, maxDate, search, orderCol, orderDir);
    }

    @ResponseBody
    @GetMapping(value = "/member/list/remove")
    @ApiOperation(value = "分页多条件搜索已删除会员列表")
    public DataTablesResult getDelMemberList(
            @RequestParam(value = "draw") int draw,
            @RequestParam(value = "start") int start,
            @RequestParam(value = "length") int length,
            @RequestParam(value = "searchKey") String searchKey,
            @RequestParam(value = "minDate") String minDate,
            @RequestParam(value = "maxDate") String maxDate,
            @RequestParam(value = "search[value]") String search,
            @RequestParam(value = "order[0][column]") int orderCol,
            @RequestParam(value = "order[0][dir]") String orderDir) {

        return memberRemote.getDelMemberList(draw, start, length, searchKey, minDate, maxDate, search, orderCol, orderDir);
    }
    @ResponseBody
    @GetMapping(value = "/member/count")
    @ApiOperation(value = "获得总会员数目")
    public DataTablesResult getMemberCount() {
        return memberRemote.getMemberCount();
    }
    @ResponseBody
    @GetMapping(value = "/member/count/remove")
    @ApiOperation(value = "获得移除总会员数目")
    public DataTablesResult getRemoveMemberCount() {
        return memberRemote.getRemoveMemberCount();
    }
    @ResponseBody
    @PostMapping(value = "/member/add")
    @ApiOperation(value = "添加会员")
    public void createMember(@ModelAttribute VOMember voMember) {
         memberRemote.createMember(voMember);
    }
    @ResponseBody
    @PutMapping(value = "/member/stop/{id}")
    @ApiOperation(value = "停用会员")
    public JSONResponse stopMember(@PathVariable(value = "id") Long id) {
        return memberRemote.stopMember(id);
    }
    @ResponseBody
    @PutMapping(value = "/member/start/{ids}")
    @ApiOperation(value = "启用会员")
    public JSONResponse startMember(@PathVariable(value = "ids") Long[] ids) {
        return memberRemote.startMember(ids);
    }
    @ResponseBody
    @PutMapping(value = "/member/remove/{ids}")
    @ApiOperation(value = "移除会员")
    public JSONResponse removeMember(@PathVariable(value = "ids") Long[] ids) {
        return memberRemote.removeMember(ids);
    }
    @ResponseBody
    @DeleteMapping(value = "/member/del/{ids}")
    @ApiOperation(value = "彻底删除会员")
    public JSONResponse deleteMember(@PathVariable(value = "ids") Long[] ids) {

        return memberRemote.deleteMember(ids);
    }
    @ResponseBody
    @PostMapping(value = "/member/change-password/{id}")
    @ApiOperation(value = "修改会员密码")
    public JSONResponse changeMemberPassword(@PathVariable(value = "id") Long id, @ModelAttribute VOMember voMember) {
        return memberRemote.changeMemberPassword(id, voMember);
    }
    @ResponseBody
    @PostMapping(value = "/member/update/{id}")
    @ApiOperation(value = "修改会员信息")
    public JSONResponse updateMember(@PathVariable(value = "id") Long id, @ModelAttribute VOMember voMember) {
        return memberRemote.updateMember(id, voMember);
    }
    @ResponseBody
    @GetMapping(value = "/member/{id}")
    @ApiOperation(value = "通过ID获取会员信息")
    public Result<Member> getMemberById(@PathVariable(value = "id") Long id) {
        return memberRemote.getMemberById(id);
    }
    @ResponseBody
    @GetMapping(value = "/member/username")
    @ApiOperation(value = "验证注册名是否存在")
    public Boolean validateUsername(@RequestParam(value = "username") String username) {
        return memberRemote.validateUsername(username);
    }
    @ResponseBody
    @GetMapping(value = "/member/phone")
    @ApiOperation(value = "验证注册手机是否存在")
    public Boolean validatePhone(@RequestParam(value = "phone") String phone) {
        return memberRemote.validatePhone(phone);
    }
    @ResponseBody
    @GetMapping(value = "/member/email")
    @ApiOperation(value = "验证注册邮箱是否存在")
    public Boolean validateEmail(@RequestParam(value = "email") String email) {
        return memberRemote.validateEmail(email);
    }
    @ResponseBody
    @GetMapping(value = "/member/edit/{id}/username")
    @ApiOperation(value = "验证编辑用户名是否存在")
    public Boolean validateEditUsername(@PathVariable(value = "id") Long id, @RequestParam(value = "username") String username) {
        return memberRemote.validateEditUsername(id, username);
    }
    @ResponseBody
    @GetMapping(value = "/member/edit/{id}/phone")
    @ApiOperation(value = "验证编辑手机是否存在")
    public Boolean validateEditPhone(@PathVariable(value = "id") Long id, @RequestParam(value = "phone") String phone) {
        return memberRemote.validateEditPhone(id, phone);
    }
    @ResponseBody
    @GetMapping(value = "/member/edit/{id}/email")
    @ApiOperation(value = "验证编辑邮箱是否存在")
    public Boolean validateEditEmail(@PathVariable(value = "id") Long id, @RequestParam(value = "email") String email) {
        return memberRemote.validateEditEmail(id, email);
    }
}
